package utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.content.Context;
import android.widget.Toast;

import com.tandil.seo.R;

public class PersistenceData {
	
    public static void writePatentes(Context context, String[] patentes, String fileName) {
    		    		
            FileOutputStream fos = null;
            OutputStreamWriter osw = null;
            String patentesArch = "";
            
            //Setea el archivo con una cadena del tipo "ALGO,ALGO,ALGO,ALGO,"
            for (int i = 0; i < patentes.length; i++){
            	patentesArch += patentes[i];
            	patentesArch += ",";
            }
            for (int x = patentes.length; x < 4; x++){
            	patentesArch += ",";
            }
            
            try {
                    fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
                    osw = new OutputStreamWriter(fos);
                    osw.write(patentesArch);
                    osw.flush();
                    
            } catch (Exception e) {
                    //Toast.makeText(context, R.string.dataNotSaved, Toast.LENGTH_SHORT).show();
            } finally {
                    try {
                            osw.close();
                            fos.close();
                    } catch (IOException e) {
                            e.printStackTrace();
                    }
            }
    }

    public static String[] readPatentes(Context context, String fileName) {

            FileInputStream fis = null;
            InputStreamReader isr = null;
            char[] inputBuffer = new char[255];
            String data = "";
            String[] patentesArch= {"", "", "", ""} ; 
            try {
                    fis = context.openFileInput(fileName);
                    isr = new InputStreamReader(fis);
                    isr.read(inputBuffer);

                    data = new String(inputBuffer);                 
            }

            catch (Exception e) {
            		writePatentes(context, patentesArch, fileName);
                    //Toast.makeText(context, R.string.dataNotRead, Toast.LENGTH_SHORT).show();
            } finally {
                    try {
                            if (fis != null) {
                                    isr.close();
                                    fis.close();
                            }
                    } catch (IOException e) {
                            e.printStackTrace();
                    }
            }
            patentesArch = data.split(",");
            return patentesArch;
    }
    
    
    public static void writeSaldo(Context context, String saldo, String fileName) {
		
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try {
                fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
                osw = new OutputStreamWriter(fos);
                osw.write(saldo);
                osw.flush();
                
        } catch (Exception e) {
                //Toast.makeText(context, R.string.dataNotSaved, Toast.LENGTH_SHORT).show();
        } finally {
                try {
                        osw.close();
                        fos.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
    }
    
    public static String readSaldo(Context context, String fileName) {

        FileInputStream fis = null;
        InputStreamReader isr = null;
        char[] inputBuffer = new char[255];
        String data = "0"; 
        try {
                fis = context.openFileInput(fileName);
                isr = new InputStreamReader(fis);
                isr.read(inputBuffer);
                data = new String(inputBuffer);                 
        }

        catch (Exception e) {
        		writeSaldo(context, data, fileName);
                //Toast.makeText(context, R.string.dataNotRead, Toast.LENGTH_SHORT).show();
        } finally {
                try {
                        if (fis != null) {
                                isr.close();
                                fis.close();
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
        return data;
    }

    public static void writeHoras(Context context, String horas, String fileName) {

        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try {
            fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            osw = new OutputStreamWriter(fos);
            osw.write(horas);
            osw.flush();

        } catch (Exception e) {
            //Toast.makeText(context, R.string.dataNotSaved, Toast.LENGTH_SHORT).show();
        } finally {
            try {
                osw.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String readHoras(Context context, String fileName) {

        FileInputStream fis = null;
        InputStreamReader isr = null;
        char[] inputBuffer = new char[25];
        String data = "0";
        try {
            fis = context.openFileInput(fileName);
            isr = new InputStreamReader(fis);
            isr.read(inputBuffer);
            data = new String(inputBuffer);
        }

        catch (Exception e) {
            writeSaldo(context, data, fileName);
            //Toast.makeText(context, R.string.dataNotRead, Toast.LENGTH_SHORT).show();
        } finally {
            try {
                if (fis != null) {
                    isr.close();
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data;
    }
    
    
    
    
}